package com.activenetwork.smlite.testcase.daily;

import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.model.event.EventType;
import com.activenetwork.schema.model.rfp.RfpBasicType;
import com.activenetwork.schema.model.rfp.RfpType;
import com.activenetwork.schema.testcase.daily.checkrfppart.CheckRfpPartDataType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.business.RfpBusiness;
import com.activenetwork.smlite.page.event.EventsHomeView;
import com.activenetwork.smlite.page.event.EventDetailView.EventSourceSubview;
import com.activenetwork.smlite.page.generic.ModelPage;
import com.activenetwork.smlite.page.rfp.RfpBasicsSubview;
import com.activenetwork.smlite.page.rfp.ViewRFPDetailTemplate;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpAdditionalRequest;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpEventSpace;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpFoodBeverage;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpReview;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpSetting;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpSleepingRoom;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpVendorSearch;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.WindowLoader;
import com.activenetwork.util.jaxb.ScriptDataBindHelper;
import com.activenetwork.util.nexuserror.NexusErrors;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class CheckRfpPart extends BaseCase{
	
	static final Logger logger = LoggerFactory.getLogger(CheckRfpPart.class);
	private EventBusiness eventBusiness = new EventBusiness();
	private RfpBusiness rfpBusiness = new RfpBusiness();
	private CheckRfpPartDataType data;
	
	@BeforeClass(alwaysRun = true)
	public void dataSetup() {	
		try{
			ScriptDataBindHelper helper = new ScriptDataBindHelper(CheckRfpPart.class);
			data = helper.bind(CheckRfpPartDataType.class);
		}catch(IOException e){
			throw new RuntimeException("Unable to bind the script data for " + this.getClass().getName());
		}
	}
	
	@BeforeClass(alwaysRun = true, dependsOnMethods = {"dataSetup"})
	public void login(){
		loginBusiness.login();
	}
	
	@Test(groups = { "daily" })
	public void createDailyRfp(){
		// Go to event home view page
		EventsHomeView eventsHomeView = PageLoader.load(EventsHomeView.class);
		eventsHomeView.events();
		
		EventType event = data.getEvent();
		RfpType rfp = data.getRfp();
		
		logger.info("Make sure the specific event exist for creating rfp");
		String searchContent = event.getBasic().getTitle();
		try{
			eventBusiness.searchEvent(searchContent, event);
		}catch(RuntimeException e){
			logger.info("The event does not exist, we need to create it first");
			eventBusiness.addEvent(event);
		}
		
		PageLoader.load(ModelPage.class).events();
		
		logger.info("Create daily rfp");
		rfpBusiness.createRfp(event, rfp);
		
		// Go back to check the created rfp in the grid
		EventSourceSubview eventSourceSubview = PageLoader.load(EventSourceSubview.class);
		try{
			eventSourceSubview.checkRfpFromGrid(rfp);
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					CheckRfpPart.class.getName(), 
					"Verify create daily rfp", 
					"Create daily rfp failed"));
		}
	}
	
	@Test(groups = { "daily" }, dependsOnMethods = {"createDailyRfp"})
	public void deleteDailyRfp(){	
		EventType event = data.getEvent();
		RfpType rfp = data.getRfp();
		
		// Go back to event home page
		PageLoader.load(ModelPage.class).events();
		logger.info("Delete daily rfp");
		rfpBusiness.deleteRfp(event, rfp);
		
		// Verify alert for sent rfp can not be deleted popped up
		NexusErrors errors = new NexusErrors("nexus-errors-EventSourceSubview");
		if(!errors.isErrorDisplay()){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					CheckRfpPart.class.getName(), 
					"Verify unable to delete sent rfp", 
					"The error message of deleting sent rfp does not display"));
		}
		
	}
	
	@Test(groups = { "daily" })
	public void createRfpDraft(){
		// Go to event home view page
		EventsHomeView eventsHomeView = PageLoader.load(EventsHomeView.class);
		eventsHomeView.events();
		
		EventType event = data.getEvent();
		RfpType rfp = data.getRfp();
		
		logger.info("Make sure the specific event exist for creating rfp");
		String searchContent = event.getBasic().getTitle();
		try{
			eventBusiness.searchEvent(searchContent, event);
		}catch(RuntimeException e){
			logger.info("The event does not exist, we need to create it first");
			eventBusiness.addEvent(event);
		}
		
		PageLoader.load(ModelPage.class).events();
		
		logger.info("Create daily rfp draft");
		rfpBusiness.createRfpDraft(event, rfp);
		
		// Go back to check the created rfp draft in the grid
		EventSourceSubview eventSourceSubview = PageLoader.load(EventSourceSubview.class);
		try{
			// the rfp draft name has "Draft: " prefix
			String rfpDraftName = "Draft: " + rfp.getBasic().getRfpName();
			RfpType rfpDraft = new RfpType();
			RfpBasicType rfpDraftBasic = new RfpBasicType();
			rfpDraftBasic.setRfpName(rfpDraftName);
			rfpDraft.setBasic(rfpDraftBasic);

			eventSourceSubview.checkRfpFromGrid(rfpDraft);
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					CheckRfpPart.class.getName(), 
					"Verify create daily rfp draft", 
					"Create daily rfp draft failed"));
		}
	}
	
	@Test(groups = { "daily" }, dependsOnMethods = {"createRfpDraft"})
	public void deleteRfpDraft(){
		EventType event = data.getEvent();
		RfpType rfp = data.getRfp();
		
		// Go back to event home page
		PageLoader.load(ModelPage.class).events();		
		logger.info("Delete daily rfp draft");
		String rfpDraftName = "Draft: " + rfp.getBasic().getRfpName();
		RfpType rfpDraft = new RfpType();
		RfpBasicType rfpDraftBasic = new RfpBasicType();
		rfpDraftBasic.setRfpName(rfpDraftName);
		rfpDraft.setBasic(rfpDraftBasic);
		rfpBusiness.deleteRfp(event, rfpDraft);
		
		// Verify alert for sent rfp can not be deleted popped up
		NexusErrors errors = new NexusErrors("nexus-errors-EventSourceSubview");
		if(errors.isErrorDisplay()){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					CheckRfpPart.class.getName(), 
					"Verify able to delete rfp draft", 
					"The error message of deleting draft rfp display"));
		}
		
	}
	
	//@Test
	public void verifyViewRfpDetail(){
		// Go back to event home page
		PageLoader.load(ModelPage.class).events();
		rfpBusiness.searchRfp("DailyRunRfpEvent", "Draft: DailyRfp");
		RfpSetting rfpSetting = PageLoader.load(RfpSetting.class);
		RfpBasicsSubview nextPage = rfpSetting.clickNext();
		
		if(nextPage instanceof RfpSleepingRoom){
			nextPage = nextPage.clickNext();
			System.out.println("sr");
		}
		if(nextPage instanceof RfpEventSpace){
			nextPage = nextPage.clickNext();
			System.out.println("es");
		}
		if(nextPage instanceof RfpFoodBeverage){
			nextPage = nextPage.clickNext();
			System.out.println("fb");
		}
		if(nextPage instanceof RfpAdditionalRequest){
			nextPage = nextPage.clickNext();
			System.out.println("ar");
		}
		if(nextPage instanceof RfpVendorSearch){
			nextPage = nextPage.clickNext();
			System.out.println("vendor search");
		}
		if(nextPage instanceof RfpReview){
			logger.info("Set Rfp Review page");
			WebDriver driver = EnvContext.webDriverTL.get();
			RfpReview rfpReview = (RfpReview)nextPage;
			// Open view rfp detail page
			WindowLoader windowLoader = new WindowLoader(driver.getWindowHandle());
			ViewRFPDetailTemplate rfpDetail = rfpReview.viewRfpDetail(windowLoader);
			logger.info(rfpDetail.getPageHeader());
			rfpReview = rfpDetail.close(windowLoader);
		}
		
	}
	
	@AfterClass
	public void logoff(){
		loginBusiness.logout();
		FailureAssertion.pop(CheckRfpPart.class);
	}
	

}
